package demo2;

public class PeopleText2 {
    public static void main(String[] args) {

        People[] arr = new People[3];
        People p1 = new People(01, "张三", 22);
        People p2 = new People(02, "李四", 23);
        People p3 = new People(03, "五", 23);
        arr[0] = p1;
        arr[1] = p2;
        arr[2] = p3;
        //要求5：查询数组id为02的学生信息，如果存在，则将他的年龄+1岁
        int index= getIndex(arr,04);
        if(index>=0){
            //存在，02id年龄＋1
            People peo= arr[index];
            int newAge =peo.getAge()+1;
            //再把加完之后的年龄放回数组
            peo.setAge(newAge);
            printArr(arr);
        }else {
            System.out.println("该id不存在");
        }

    }
    public static int getIndex(People[]arr,int id){
        for (int i = 0; i < arr.length; i++) {
            People peo = arr[i];
            if(peo!=null){
                int pid=peo.getId();
                if(pid==id){
                    return i;
                }
            }
        }return -1;
    }
    public static void printArr(People[]arr){
        for (int i = 0; i < arr.length; i++) {
            People p=arr[i];
            if(p!=null){
                System.out.println(p.getId()+" "+p.getName()+" "+p.getAge());
            }
        }
    }
}
